package com.exercises;

import com.exercises.common.TreeNode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;

class Exercise938 {

    List<Integer> list = new ArrayList<>();
    int low, high;


    public int rangeSumBST(TreeNode root, int low, int high) {
        this.low = low;
        this.high = high;
        dfs(root);
        return list.stream().mapToInt(Integer::intValue).sum();
    }

    public void dfs(TreeNode node) {
        if (node == null) return;
        dfs(node.right);
        if (node.val >= low && node.val <= high) {
            list.add(node.val);
        }
        if (node.val > low) {
            dfs(node.left);
        }
    }
}
